﻿@using KPIS.GERP.Models.ViewModel
@using KPIS.GERP.Models.ICM
@model IEnumerable<PurchaseRequest>

@{
    ViewBag.Title = "รายการขอซื้อ/จ้าง [PR]";
    ViewBag.Descr = "รายการขอซื้อ ขอจ้าง ที่ทางพัสดุทำรายการจากการแจ้งความประสงค์ฯ";
}

@if(!string.IsNullOrWhiteSpace(ViewBag.SuccessMessage) )
{
    <div class="alert alert-success" style="margin-top: 24px">
        <button type="button" class="close" data-dismiss="alert">
            <i class="icon-remove"></i>
        </button>
        <strong>สำเร็จ!</strong>

        @ViewBag.SuccessMessage
    </div>
}
@if(!string.IsNullOrWhiteSpace(ViewBag.ErrorMessage) )
{
    <div class="alert alert-error" style="margin-top: 24px">
        <button type="button" class="close" data-dismiss="alert">
            <i class="icon-remove"></i>
        </button>
        <strong>ข้อผิดพลาด!</strong>

        @ViewBag.ErrorMessage
    </div>
}

@using (Html.BeginForm("DeleteAll", "Purchase", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    
    <p id="message"></p>
    
    <div class="row-fluid">    
        <div class="block-button-menu-above">
            @*@Html.ButtonAdd()*@
            @Html.ButtonDeleteAll()
        </div>

      <div class="alert-error">@Html.ValidationSummary()</div>

        <div class="row-fluid">
            <div class="table-header span12">
                <div class="span10">
					@ViewBag.Title
                </div>
				@Html.Partial("_ExportButton")
            </div>
        </div>

        <table id="table" class="table table-striped table-bordered table-hover" aria-describedby="table_info">
            <thead>
                <tr>
                    <th class="center" style="width:24px;">
						<!-- Check box delete multiple records column-->
                        <label>
                            <input type="checkbox" />
                            <span class="lbl"></span>
                        </label>
                    </th>
                    <th style="width:100px;text-align: center;">
                        เลขขอซื้อ/จ้าง
                    </th>
                    <th style=" text-align: center">
                        @Html.DisplayNameFor(model => model.Name)
                    </th>
                    <th style="width:100px;text-align: center;">
                        @*@Html.DisplayNameFor(model => model.MasPurchaseMethod.Name)*@
                        วิธีการจัดหา
                    </th>
                    <th style="width:80px;text-align: center;">
                        หมายเหตุ
                    </th> 
                    <th style="width:80px;text-align: center;">
						 @KPIS.GERP.Models.Class.LookupUtility.GetLookupNameFromAllLookup(KPIS.GERP.Models.ICM.IcmMasLookUpType.LookUpType.Priority)
					</th>
                    <th style="width:100px;text-align: center;">
                         @Html.DisplayNameFor(model => model.RecordStatus)
                    </th>
					<th class="center" style="width:20px;">
						<!-- Empty head column for export buttons -->&nbsp;
					</th>
                </tr>
            </thead>

            <tbody>
                @foreach(var m in (Model??new List<PurchaseRequest>()).OrderByDescending(o=>o.PrId))
                {
                    <tr>
                        <td class="center">
							<!-- Check box all for delete multiple records -->
                            @if (new List<string>{RecordStatus.ApprovedStatus, RecordStatus.ProcessStatus}.Contains(m.RecordStatus)){
                            <label>
                                <input type="checkbox" name="deleteIds" value="@(m.PrId)" disabled />
                                <span class="lbl"></span>
                            </label>
                            }else{
                            <label>
                                <input type="checkbox" name="deleteIds" value="@(m.PrId)" />
                                <span class="lbl"></span>
                            </label>
                            }
                        </td>
                        <td style="text-align:center">
							@Html.ActionLink(@m.Code, "Details", new { Id = m.PrId, _ = m.Code + "-" + (m.Name ?? "").Trim().Replace(" ", "") }) 
						</td>
						<td style="font-weight: bold">
							@Html.ActionLink(string.IsNullOrWhiteSpace(m.Name) ? "-" : m.Name, "Details", new { Id = m.PrId, _ = m.Code + "-" + (m.Name ?? "").Trim().Replace(" ", "") }) 
						</td>
						<td style="text-align:center">
							@Html.DisplayFor(model => m.MasPurchaseMethod.Name)
						</td>
						<td style="text-align:center">
							@Html.DisplayFor(model => m.Remark)
                            @*<span style="display: none">@(m.PurchaseDate.HasValue?m.PurchaseDate.Value.ToDbFormat():"")</span>@(m.PurchaseDate.HasValue?m.PurchaseDate.Value.ToString("dd/MM/yyyy"):"")*@
						</td>
						<td style="text-align:center"> 
                            @if (m.Priority.LookUpName == "ปกติ")
                            {
                                @Html.Raw("<font color='green'>")
                            }
                            else
                            {
                                @Html.Raw("<font color='red'>")
                            }
                            @Html.DisplayFor(model => m.Priority.LookUpName)
                            @Html.Raw("</font>")
						</td>
                        <td style="text-align: center; font-weight: bold">
                            <span class="recordstatus-@m.RecordStatus">
                                @RecordStatus.RecordStatusName(m.RecordStatus)
                            </span>
                        </td>
                        <td class="td-actions">
							@if (m.RecordStatus == KPIS.GERP.Models.SYS.RecordStatus.WaitStatus || m.RecordStatus == KPIS.GERP.Models.SYS.RecordStatus.ReturnedStatus)
       {
							    @Html.Partial("_ButtonMenuEachRow", new ExportGrid
      {
          Id = m.PrId,
          DetailsUrl = Url.Action("Details", new { Id=m.PrId, nolayout = false, ViewOnly = false }),
          EditUrl = Url.Action("Edit") + "/" + m.PrId,
          DeleteUrl = Url.Action("Delete") + "/" + m.PrId
      })
       }
       else
       {
                                @Html.Partial("_ButtonMenuEachRow", new ExportGrid
                           {
                               Id = m.PrId,
                               DetailsUrl = Url.Action("Details", new { Id=m.PrId, nolayout = false, ViewOnly = false })
                           })
       }
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
}

@section scripts {
    
	<script type="text/javascript">
	    $(function () {
	        //$("#table")
            //    .dataTable({
            //        "aaSorting": [[0, "desc"]]
            //    });
			var oTable1 = $('#table').dataTable({
				"aoColumns": [
                    { "bSortable": false },
                    null,
                    null,
                    null,
                    null, 
                    null,
                    null,
                    { "bSortable": false }
				]
			});

			$('table th input:checkbox').on('click', function () {
				var that = this;
			    $(this).closest('table').find('tr > td:first-child input:checkbox').not(":disabled")
				.each(function () {
					this.checked = that.checked;
					$(this).closest('tr').toggleClass('selected');
				});
			});

			$('[data-rel=tooltip]').tooltip();

			/*$("form").submit(function () {
				var currentForm = this;
				bootbox.confirm(function (result) {
					var _result = result;
					if (result) {
						$.ajax({
							url: "DeleteAll",
							type: "post",
							dataType: "json",
							data: $(currentForm).serialize(),
							success: function (result) {
							    alert(result);
								if (result.success) {
									location.reload();
								}
								else {
								    $("#message").html("พบข้อผิดพลาดไม่สามารถลบรายการได้");
								    
								}
							}
						});
					}
				})
				return false;
			});*/
		})

	</script>

}